package co.yixiang.modules.trace.serviceImpl;

import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.adopt.entity.domain.YxAdoptContract;
import co.yixiang.modules.trace.entity.domain.TraceBreed;
import co.yixiang.modules.trace.entity.dto.TraceBreedQueryCriteria;
import co.yixiang.modules.trace.entity.vo.TraceBreedVo;
import co.yixiang.modules.trace.mapper.TraceBreedMapper;
import co.yixiang.modules.trace.service.TraceBreedService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author 13409
 */

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true,rollbackFor = Exception.class)
public class TraceBreedServiceImpl extends BaseServiceImpl<TraceBreedMapper, TraceBreed> implements TraceBreedService {
    @Autowired
    private IGenerator generator;

    @Override
    public Map<String, Object> queryAllByVo(TraceBreedQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<TraceBreed> page = new PageInfo<>(baseMapper.selectList(QueryHelpPlus.getPredicate(TraceBreed.class, criteria)));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), TraceBreedVo.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public Map<String, Object> queryAll(TraceBreed criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<TraceBreed> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<TraceBreed> queryAll(TraceBreed criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(TraceBreed.class, criteria));
    }

    @Override
    public void delete(Integer id) {
        this.removeById(id);
    }

    @Override
    public void update(TraceBreed resources) {
        this.updateById(resources);
    }

    @Override
    public TraceBreed create(TraceBreed resources) {
        resources.setOnlyKey(UUID.randomUUID().toString().replaceAll("-",""));
        this.save(resources);
        return resources;
    }
}
